package com.steve.ai.example;

import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.HTreeMap;
import org.mapdb.Serializer;

import java.io.File;

/**
 * @Author: STEVE
 * @Description: 示例 - MapDB
 * @since: 2025-07-14
 */
public class MapDBExample {

    public static void main(String[] args) {
        // 创建或打开一个
        File dbFile = new File("mydb");
        DB db = DBMaker.fileDB(dbFile).make();
        HTreeMap<String, String> map = db.hashMap("myMap")
                .keySerializer(Serializer.STRING)
                .valueSerializer(Serializer.STRING)
                .createOrOpen();
        map.put("key1", "value1");
        map.put("key2", "value2");
        // 提交事务以确保数据被持久化到磁盘
        db.commit();
        // 关闭数据库连接
        db.close();
        // 重新打开数据库以检索数据
        db = DBMaker.fileDB(dbFile).make();
        map = db.hashMap("myMap")
                .keySerializer(Serializer.STRING)
                .valueSerializer(Serializer.STRING)
                .createOrOpen();
        // 从Map中检索数据
        String value1 = map.get("key1");
        String value2 = map.get("key2");
        System.out.println("Value for key1: " + value1);
        System.out.println("Value for key2: " + value2);
        // 关闭数据库连接
        db.close();
    }

}
